var actcouponbill = require('../../model/coupon_cont')
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    showmore: false,
    loading: false,
    isload: true,
    modellist: [],
    isinit: false,
    showlabel: false,
    actcoupon: null,
    activetab: "-1",
    orderinfo: null,
    vcode:"",
  },
  /**
   * 组件的方法列表
   */
  methods: {
    couponshow() {
      this.triggerEvent('click', {})
    },

    counoncodechange(ev){
      this.setData({vcode:ev.detail})
      

    },
    exchange(ev){

      var vcode="";
      if(typeof(ev.detail)=='string'){
        vcode=ev.detail;
      }
      if(vcode==""){
        vcode =this.data.vcode
      }

      wx.showToast({
        icon: 'none',
        title: '验证码无效',
      })

    
     
    },
    initcoupon(acttab) {

      if (!acttab) {
        acttab = "-1"
      }
      //初始化优惠券
      //this.getdata();
      var that = this;
      var updatemodel = {}
      updatemodel['activetab'] = acttab
      updatemodel["isinit"] = true
      that.setData(updatemodel)
      this.getpagelist(that, 1, { itype: acttab })

    },
    showinfo() {
      var app = getApp();
      this.setData({ showmore: true, CustomBar: app.globalData.CustomBar })
    
      if (this.data.isinit == false) {
        this.initcoupon();
      }
    },
    onReachBottom() {
      var that = this
      that.getpagelist(that, -1)
      //触底分页
    },
    morecoupon() {
      this.setData({ showmore: !this.data.showmore })
    },
    selecteditem(e) {

      var app = getApp()
      var homeurl = app.globalData.configdata.basic.homeurl
      wx.reLaunch({
        url: homeurl,
      })

      return


      var actcoupon = this.data.actcoupon
      var dataset = e.currentTarget.dataset
      var item = dataset.item

      if (actcoupon == null || actcoupon.bindid != item.bindid) {
        this.setData({ actcoupon: item })

      }
      else {
        this.setData({ actcoupon: null })
      }
      this.blackdadta();
    },
    clearselected() {
      this.setData({ actcoupon: null })
      this.blackdadta();
    },

    //
    tabschanges(ev) {
      var that = this
      that.setData({ activetab: ev.detail.name })
      //this.data.activetab=  ev.detail.name
      that.getpagelist(that, 1, { itype: ev.detail.name })

    },
    //回调选择数
    blackdadta() {
      this.triggerEvent('couponblack', this.data.actcoupon)
      this.setData({ showmore: false })
    },
    Backfunc() {
      //关闭优惠券触发
      this.setData({ showmore: false })
    },

    getpagelist(that, rest, strwhere) {
      if (rest == 1) {
        if (!strwhere) {
          strwhere = {};
        }


        that.setData({ loading: true, modellist: [] });
      }
      // that.setData({ isload:true });

      actcouponbill.offset(that, rest, strwhere).then(function (result) {
        var updatemdoel = {};

        if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
          if (rest == 1) {
            updatemdoel["modellist"] = result["obj"]["modellist"];
          }
          else {
            if (result["obj"]["modellist"].length > 0) {
              updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
            }
          }
        }
        
        that.setData(updatemdoel)
      });
    },
    getdata() {
      var that = this;
      that.setData({ isload: true, isinit: true })
      actcouponbill.mycoupon().then(res => {
      
        if (res["status"] == 1) {
          var modellist = [];
          if (res["obj"]) {
            var temobj = res["obj"];
            var ilen = temobj.length



            if (ilen > 0) {
              modellist = temobj

              modellist = modellist.concat(JSON.parse(JSON.stringify(modellist)))
              // modellist = modellist.concat(JSON.parse(JSON.stringify(modellist)))

              ilen = modellist.length
              for (var i = 0; i < ilen; i++) {
                if (modellist[i].cid == 1) {
                  modellist[i].val = modellist[i].val
                }
                else {
                  modellist[i].val = modellist[i].val
                }
                modellist[i].fullmoney = modellist[i].fullmoney
              }
            }
          }
          else {
          }
          that.setData({ isload: false, modellist: modellist })
        }
      })
    }
  }
})
